package com.one.efaimaly.user;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.CMemoryData;
import com.one.common.config.RouterPath;
import com.one.common.manager.imagchoose.ChooseManager;
import com.one.common.manager.imagchoose.IChooseImg;
import com.one.common.manager.imageloader.ILoader;
import com.one.common.manager.imageloader.LoaderManager;
import com.one.common.model.bean.CityBean;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.utils.StringUtils;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.addresspick.AddressSelector;
import com.one.common.view.addresspick.CityInterface;
import com.one.common.view.addresspick.OnItemClickListener;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.widget.HorizontalMenuItem;
import com.one.common.view.widget.MyTitleBar;
import com.one.efaimaly.R;
import com.one.common.model.bean.UserWorkerBean;
import com.one.efaimaly.user.presenter.UserWorkerPresenter;
import com.one.efaimaly.user.view.UserWorkerView;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 上午10:10
 * description ：完善资料 用户信息
 **/
@Route(path = RouterPath.USER_INFO)
public class UserInfoActivity extends BaseActivity<UserWorkerPresenter> implements UserWorkerView {
    @BindView(R.id.hmi_avatar)
    HorizontalMenuItem hmiAvatar;
    @BindView(R.id.hmi_name)
    HorizontalMenuItem hmiName;
    @BindView(R.id.hmi_phone)
    HorizontalMenuItem hmiPhone;
    @BindView(R.id.hmi_address)
    HorizontalMenuItem hmiAddress;
    @BindView(R.id.hmi_contact)
    HorizontalMenuItem hmiContact;
    @BindView(R.id.hmi_contact_phone)
    HorizontalMenuItem hmiContactPhone;
    private UserInfoBean userInfoBean;
    private UserWorkerBean userWorkerBean;

    private File avatarFile;
    private ILoader.Options options;
    private String avatarUrl;
    /**
     * 选择城市
     */
    private SelectCityDialog selectCityDialog;
    private ArrayList<CityBean> cityBeans = new ArrayList<>();
    /**
     * 省 市 区
     */
    private CityBean cityBean1 = new CityBean(), cityBean2 = new CityBean(), cityBean3 = new CityBean();

    @Override
    protected void initPresenter() {
        mPresenter = new UserWorkerPresenter(this, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE_RTEXT)
                .setTitleText("完善资料")
                .setRightText("编辑");
    }

    @Override
    public void initData() {
        super.initData();
        userInfoBean = CMemoryData.getUserInfo();
        setUserInfo();
        mPresenter.getUserWorkerInfo();
    }

    @Override
    public void initView() {
        super.initView();
    }

    /**
     * 设置用户信息
     */
    private void setUserInfo() {
        if (userInfoBean != null) {
            if (StringUtils.isNotBlank(userInfoBean.getMember_img())) {
                LoaderManager.getLoader().loadNet(hmiAvatar.getIvRight(), userInfoBean.getMember_img(), getOptions());
            }
            hmiName.setRightTxt(userInfoBean.getMember_real_name());
            hmiPhone.setRightTxt(userInfoBean.getMember_account());
        }
    }

    private ILoader.Options getOptions() {
        if (options == null) {
            options = new ILoader.Options(R.mipmap.ic_avatar_default, R.mipmap.ic_avatar_default, true);
        }
        return options;
    }

    @Override
    public void setUserWorkerView(UserWorkerBean bean) {
        if (bean != null) {
            userWorkerBean = bean;
            hmiAddress.setRightTxt(bean.getMember_provider() + " " + bean.getMember_city() + " " + bean.getMember_country());
            hmiContact.setRightTxt(bean.getMember_name());
            hmiContactPhone.setRightTxt(bean.getMember_phone());
        }
    }

    @Override
    public void setCityView(ArrayList<CityBean> arrayList) {
        cityBeans.addAll(arrayList);
        showSelectCity();
    }

    @Override
    public void setAvatarUrl(String avatar) {
        avatarUrl = avatar;
        LoaderManager.getLoader().loadNet(hmiAvatar.getIvRight(), avatar, getOptions());
    }

    @Override
    protected void onClickRightText(View v) {
        super.onClickRightText(v);
        TextView textView = (TextView) v;
        if (textView.getText().equals("编辑")) {
            ((TextView) v).setText("保存");
            hmiContactPhone.setIsEdit(true);
            hmiContact.setIsEdit(true);
            hmiName.setIsEdit(true);
            hmiAvatar.setIsEnabled(true);
            hmiAddress.setIsEnabled(true);
        } else {
            update();
        }
    }

    /**
     * 修改
     */
    private void update(){
        mPresenter.update(avatarUrl,
                hmiName.getEditRight().getText().toString(),
                hmiContact.getEditRight().getText().toString(),
                hmiContactPhone.getEditRight().getText().toString(),
                cityBean1.getCityName(),
                cityBean2.getCityName(),
                cityBean3.getCityName());
    }


    @OnClick(R.id.hmi_avatar)
    public void toSelectPic() {
        ChooseManager.getChooser().chooseSingle(this);
    }

    @OnClick(R.id.hmi_address)
    public void toSelectCity() {
        if (ListUtils.isEmpty(cityBeans)) {
            mPresenter.getCitys();
        } else {
            showSelectCity();
        }
    }

    private void showSelectCity() {
        selectCityDialog = new SelectCityDialog(this);
        if (!ListUtils.isEmpty(cityBeans)) {
            selectCityDialog.setCitys(cityBeans);
        }
        selectCityDialog.setOnTabSelectedListener(new AddressSelector.OnTabSelectedListener() {
            @Override
            public void onTabSelected(AddressSelector addressSelector, AddressSelector.Tab tab) {
                switch (tab.getIndex()) {
                    case 0:
                        // 省
                        addressSelector.setCities(cityBeans);
                        break;
                    case 1:
                        // 市
                        addressSelector.setCities(cityBean1.getCityBeans());
                        break;
                    case 2:
                        // 区
                        addressSelector.setCities(cityBean2.getCityBeans());
                        break;
                }
            }

            @Override
            public void onTabReselected(AddressSelector addressSelector, AddressSelector.Tab tab) {

            }
        });
        selectCityDialog.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void itemClick(AddressSelector addressSelector, CityInterface city, int tabPosition) {
                switch (tabPosition) {
                    case 0:
                        // 省
                        cityBean1 = mPresenter.getCityBean(cityBeans, city.getCityName());
                        addressSelector.setCities(cityBean1.getCityBeans());
                        break;
                    case 1:
                        // 市
                        cityBean2 = mPresenter.getCityBean(cityBean1, city.getCityName());
                        addressSelector.setCities(cityBean2.getCityBeans());
                        break;
                    case 2:
                        // 区
                        cityBean3 = mPresenter.getCityBean(cityBean2, city.getCityName());
                        hmiAddress.setRightTxt(cityBean1.getName()+" "+cityBean2.getName()+" "+cityBean3.getName());
                        selectCityDialog.dismiss();
                        break;
                }
            }
        });
        selectCityDialog.show();

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (data != null) {
                ArrayList<String> pathList =
                        data.getStringArrayListExtra(IChooseImg.EXTRA_RESULT_SELECTION);
                if (pathList != null && pathList.size() > 0) {
                    avatarFile = new File(pathList.get(0));
                    LoaderManager.getLoader().loadFile(hmiAvatar.getIvRight(), avatarFile, getOptions());
                    mPresenter.uploadImg(avatarFile);
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (selectCityDialog != null && selectCityDialog.isShowing()) {
            selectCityDialog.dismiss();
        }
    }
}
